﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IXION.BLOG.WEB.UI.UserControl
{
    /// <summary>
    /// 分页条控件
    /// 本分页控件基于bootstrap制作
    /// </summary>
    public partial class Pager : System.Web.UI.UserControl
    {
        /// <summary>
        /// 当前页码
        /// </summary>
        public int curPage = 1;
        /// <summary>
        /// 分页路径
        /// </summary>
        public string pageUrl = "Default.aspx?page=";
        /// <summary>
        /// 总页数
        /// </summary>
        public int pageCount = 10;
        /// <summary>
        /// 分页条长度
        /// </summary>
        public int length = 5;
        protected void Page_Load(object sender, EventArgs e)
        {
            PagerLable.Text = GenPagerBar();
        }
        private string GenPagerBar()
        {
            if(pageCount == 0){
                return "";
            }
            //超出总页数
            if (curPage > pageCount)
            {
                curPage = pageCount;
            }
            //负数页码
            if (curPage < 0)
            {
                curPage = 1;
            }
            string target = "<ul class='pagination pagination-sm fill-inline'>";
            //上一页
            target += "<li " + (curPage <= 1 ? "class='disabled'" : "") + "><a href='" + pageUrl + (curPage - 1) + "'>&laquo;</a></li>";
            //总页数不够长度
            if (pageCount < length)
            {
                for (int i = 1; i <= pageCount; i++)
                {
                    target += "<li " + (curPage == i ? "class='active'" : "") + "><a href='" + pageUrl + i + "'>" + i + (curPage == i ? "<span class='sr-only'>(current)</span>" : "") + "</a></li>";
                }
            }
            ///最后几页
            else if (curPage + length / 2 > pageCount)
            {
                for (int i = pageCount - length; i <= pageCount; i++)
                {
                    target += "<li " + (curPage == i ? "class='active'" : "") + "><a href='" + pageUrl + i + "'>" + i + (curPage == i ? "<span class='sr-only'>(current)</span>" : "") + "</a></li>";
                }
            }
            else if (curPage < length) {
                for (int i = 1; i <= length; i++ )
                {
                    target += "<li " + (curPage == i ? "class='active'" : "") + "><a href='" + pageUrl + i + "'>" + i + (curPage == i ? "<span class='sr-only'>(current)</span>" : "") + "</a></li>";
                }
            }
            else{
                for (int i = curPage - length / 2; i < curPage + length / 2; i++)
                {
                    target += "<li " + (curPage == i ? "class='active'" : "") + "><a href='" + pageUrl + i + "'>" + i + (curPage == i ? "<span class='sr-only'>(current)</span>" : "") + "</a></li>";
                }
            }
            //下一页
            target += "<li " + (curPage == pageCount ? "class='disabled'" : "") + "><a href='" + pageUrl + (curPage + 1) + "'>&raquo;</a></li>";
            target += "</ul>";
            return target;
        }
    }
}